package controlleur;

import eyrolles.EmployeForm;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import modele.Employe;
import modele.InterfaceEmployeModel;
import org.apache.struts.action.ActionForm;
import org.apache.struts.actions.DispatchAction;

public class GenericAction extends DispatchAction {

    protected InterfaceEmployeModel getModel(HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        return (InterfaceEmployeModel) context.getAttribute("ModeleEmploye");
    }

    protected Employe getEmploye(ActionForm form) {
        EmployeForm eForm = (EmployeForm) form;

        Employe employe = new Employe();
        employe.setUsername(eForm.getUsername());
        employe.setPassword(eForm.getPassword());
        employe.setRoleid(Integer.parseInt(eForm.getRoleid()));
        employe.setDepid(Integer.parseInt(eForm.getDepid()));
        employe.setName(eForm.getName());
        employe.setPhone(eForm.getPhone());
        employe.setEmail(eForm.getEmail());

        return employe;
    }

    protected EmployeForm buildEmployeForm(Employe employe) {
        EmployeForm eForm = new EmployeForm();

        eForm.setUsername(employe.getUsername());
        eForm.setPassword(employe.getPassword());
        eForm.setRoleid(employe.getRoleid().toString());
        eForm.setDepid(employe.getDepid().toString());
        eForm.setName(employe.getName());
        eForm.setPhone(employe.getPhone());
        eForm.setEmail(employe.getEmail());

        return eForm;
    }
}